package nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ezy.lite.util.ContextUtil;
import ezy.lite.util.DateTime;
import ezy.lite.util.UI;
import ezy.widget.view.BannerView;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.fragment.BaseFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.main.Home;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.main.Link;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.AccountRegisterActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.GDMapActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.WebViewActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter.HomeAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.route.RouteActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.BannerLayout;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.widget.WrappingLinearLayoutManager;


/**
 * Created by Administrator on 2016/3/11 0011.
 */
public class MainHomeFragment extends BaseFragment {

    @BindView(R.id.banner)
    BannerLayout bannerLayout;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;

    @BindView(R.id.list)
    RecyclerView mListView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;
    private HomeAdapter mAdapter;


    public static List<String> photoUrl = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main_home, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, getView());
        Link link = new Link();
        link.image = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496397545712&di=946f5fefdf55251db73449c690e27d9d&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F17%2F76%2F01%2F78P58PICxNm_1024.jpg";
        link.title = "我擦";
        link.url = "www.baidu.com";
        Link link1 = new Link();

        link1.image = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496397575405&di=f33523c216445ec32c107e218b0e8753&imgtype=0&src=http%3A%2F%2Fimg.taopic.com%2Fuploads%2Fallimg%2F140118%2F234952-14011Q2513970.jpg";
        link1.title = "我擦";
        link1.url = "www.baidu.com";
        List<Link> banners = new ArrayList<>();
        banners.add(link);
        banners.add(link1);
        photoUrl.add(link.image);
        photoUrl.add(link1.image);

        bannerLayout.setViewUrls(photoUrl);
        bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                WebViewActivity.start(getContext(), banners.get(position).title, banners.get(position).url);
            }
        });
        mAdapter = new HomeAdapter(getContext());
        mListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mListView.setNestedScrollingEnabled(false);
        mListView.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(() -> {
            mRefresh.setRefreshing(true);
            load();
            mRefresh.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mRefresh.setRefreshing(false);
                }
            }, 2000);

        });
        load();
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0, 0);
            }
        });
    }

    private void load() {
        List<Home> list = new ArrayList<>();


        Home home1 = new Home();
        home1.joinNum = 3;
        home1.joinedNum = 1;
        home1.title = "【限量】VIVO手机";
        home1.imageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496385198677&di=0b3bb8133ffab7ee4c2de3eaf667f21d&imgtype=0&src=http%3A%2F%2Fimage.it168.com%2Fn%2F0x0%2F8%2F8331%2F8331681.jpg";


        Home home2 = new Home();
        home2.joinNum = 3;
        home2.joinedNum = 1;
        home2.title = "【限量】苹果6手机";
        home2.imageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496385475120&di=1eb1af6d555d6ad06de441b5b0d79052&imgtype=0&src=http%3A%2F%2Fi0.sinaimg.cn%2Fgm%2Fcr%2F2015%2F0710%2F2405271329.jpg";

        Home home3 = new Home();
        home3.joinNum = 3;
        home3.joinedNum = 1;
        home3.title = "【限量】苹果7手机";
        home3.imageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496385590569&di=ebf8f831bbe14e5cbf65372f5b4a6472&imgtype=0&src=http%3A%2F%2Fimg.files.swws.258.com%2F1%2F2016%2F1010%2F10%2F57fb01b3e38f2.jpg";

        Home home4 = new Home();
        home4.joinNum = 3;
        home4.joinedNum = 1;
        home4.title = "【限量】华为P10手机";
        home4.imageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496385642951&di=3af180c67adc49e24dd9a8cafc8d4fd3&imgtype=0&src=http%3A%2F%2Fn1.itc.cn%2Fimg8%2Fwb%2Frecom%2F2017%2F02%2F26%2F148810756001270923.JPEG";

        Home home = new Home();
        home.joinNum = 3;
        home.joinedNum = 1;
        home.title = "【限量】NO.1 S6i";
        home.imageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496385315089&di=79b235107cdd942c81e3f8d01029fc9a&imgtype=0&src=http%3A%2F%2Fpic.qiantucdn.com%2F58pic%2F18%2F10%2F55%2F55d34a6150297_1024.jpg";

        list.add(home1);
        list.add(home2);
        list.add(home3);
        list.add(home4);
        list.add(home);
        mAdapter.update(list);
    }
}
